// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
	const userInfo = event.userInfo
	const currentIndex = event.currentIndex
	const {
		cloudImgsList: imgs,
		formData,
		postTime,
		type
	} = event
	// 首页展示的图片
	const image = imgs[0]
	let funName = ''
	switch (currentIndex) {
		case 0:
			funName = 'topics_info'
			break
		case 1:
			funName = 'idles_info'
			break
		case 2:
			funName = 'runErrands_info'
			break
		case 3:
			funName = 'leases_info'
			break

	}

	if (currentIndex == 0) {
		return await db.collection(funName).add({
			data: {
				imgs,
				title: formData.title,
				content: formData.content,
				address: formData.address,
				postTime: postTime,
				openId: userInfo.openId,
				avatarUrl: userInfo.avatarUrl,
				nickName: userInfo.nickName,
				image,
				hits: 0,
				likes: 0,
				likesMenberGroup:[],
				type
			}
		})
	} else {
		return await db.collection(funName).add({
			data: {
				imgs,
				title: formData.title,
				content: formData.content,
				address: formData.address,
				price: parseInt(formData.price),
				postTime: postTime,
				openId: userInfo.openId,
				avatarUrl: userInfo.avatarUrl,
				nickName: userInfo.nickName,
				image,
				hits: 0,
				likes: 0,
				likesMenberGroup:[],
				type
			}
		})
	}

}
